<?php
/***
 * 创作蒙板启动加载列表
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Services\ImgServices\ImgTool;
use App\Services\ProdTempCategoryService\ProdTempCategoryBackService;
use App\Services\ProdTempCategoryService\ProdTempCategoryTool;
use App\Services\ProdTempCreationMasksService\ProdTempCreationMasksService;
use App\Services\ProductTempService\ProductTempService;
use App\Services\ProductTempService\ProductTempTool;
use Illuminate\Http\Request;

class ProdTempCreationMasksController extends Controller
{
     protected  $creationMasksSer;
     protected  $categoryBackSer;
     protected  $productTempSer;
    /**
     * ProdTempCreationMaksController constructor.
     */
    public function __construct(ProdTempCreationMasksService $creationMasksSer,
                                  ProductTempService $productTempSer,
                                  ProdTempCategoryBackService  $categoryBackSer)
    {
        $this->creationMasksSer=$creationMasksSer;
        $this->categoryBackSer=$categoryBackSer;
        $this->productTempSer=$productTempSer;
    }

    /**
     * 添加创作启动模板
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function addCreationMasks(Request $request){
        return view('backstage.prodTempCreationMasks.addCreationMask');
    }

    /**
     * 保存创作启动蒙板
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function saveCreationMask(Request $request){
        $rules=[
            'original_image'=>'required|image|',
            'name'=>'required|string|',
            'resource_id'=>'required|string|',
            'temp_category_id'=>'required|integer|min:1',
        ];
        $name = trim($request->input('name'));
        $original_image= $request->file('original_image');
        $resource_id= trim($request->input('resource_id'));
        $temp_category_id= intval(trim($request->input('temp_category_id')));
        $val_data['name']=$name;
        $val_data['original_image']=$original_image;
        $val_data['resource_id']=$resource_id;
        $val_data['temp_category_id']=$temp_category_id;
        $data['name']=$name;
        $data['original_image']=$original_image;
        $data['resource_id']=$resource_id;
        $data['temp_category_id']=$temp_category_id;
        $validator = ProdTempCreationMasksService::store($request,$rules);
        $validator->after(function($validator) use ($val_data)
        {
            $mask_exist =  $this->creationMasksSer->creationMaskExistByName($val_data['name']);
            if($mask_exist)
            {
                $validator->errors()->add('name', '已存在!');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();;
        }
        $res=$this->creationMasksSer->saveCreationMask($data);
        if($res){
            return redirect()->back()->with('success_message','添加成功');
        }
        return redirect()->back()->with('error_message','添加失败');
    }

    /**
     * 更新创作类型启动蒙板
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function saveUpdateCreationMask(Request $request){
        $rules=[
            'name'=>'required|string|',
            'resource_id'=>'required|string|',
            'temp_category_id'=>'required|integer|min:1',
            'id'=>'required|integer|min:1',
        ];
        $id = intval($request->input('id'));
        $name = trim($request->input('name'));
        $original_image= $request->file('original_image');
        $resource_id= trim($request->input('resource_id'));
        $temp_category_id= intval(trim($request->input('temp_category_id')));
        $val_data['id']=$id;
        $val_data['name']=$name;
        $val_data['original_image']=$original_image;
        $val_data['resource_id']=$resource_id;
        $val_data['temp_category_id']=$temp_category_id;
        $data['name']=$name;
        $data['original_image']=$original_image;
        $data['id']=$id;
        $data['resource_id']=$resource_id;
        $data['temp_category_id']=$temp_category_id;
        $validator = ProdTempCreationMasksService::store($request,$rules);
        $validator->after(function($validator) use ($val_data)
        {
            $mask_exist =  $this->creationMasksSer->creationMaskExistByName($val_data['name'],$val_data['id']);
            if($mask_exist)
            {
                $validator->errors()->add('name', '已存在!');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();;
        }
        $res=$this->creationMasksSer->saveCreationMask($data);
        if($res){
            return redirect()->back()->with('success_message','添加成功');
        }
        return redirect()->back()->with('error_message','添加失败');
    }
    /**
     * 创作启动蒙板列表
     * @param Request $request
     * @return $this|void
     */
    public function getCreationMaskList(Request $request){
        $page=intval($request->input('page'));
        if(!empty($page)){
            if(!is_int($page)||$page>50){
                return abort('404');
            }
        }
        $data['per_page']=10;
        $creation_masks=$this->creationMasksSer->getCreationMaskList($data);
        return view('backstage.prodTempCreationMasks.creationMaskList')->with('creation_masks',$creation_masks);
    }

    /**
     * 更新排序
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postUpdateSort(Request $request)
    {
        $v=array(
            'id'=>'required|integer|min:1',
            'sort'=>'required|integer|max:9999'
        );
        $validator=$this->creationMasksSer->store($request,$v);
        $id = intval(trim($request->input("id")));
        $sort= intval(trim($request->input("sort")));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                $result["state"] = -2;
                $result["message"] = "编号不正确";
                return response()->json($result);
            }
            if($validator->errors()->first('sort')){
                $result["state"] = -3;
                $result["message"] = "排序数字格式不正确";
                return response()->json($result);
            }
        }
        $creation_mask =  $this->creationMasksSer->getCreationMaskById($id);
        if(empty($creation_mask)){
            $result["state"] = -3;
            $result["message"] = "找不到数据";
            return response()->json($result);
        }
        $res =  $this->creationMasksSer->updateCreationMaskSort($id,$sort);
        if($res){
            $result["state"] = 1;
            $result["message"] = "更新成功";
            return response()->json($result);
        }else{
            $result["state"] = -1;
            $result["message"] = "更新失败";
            return response()->json($result);
        }
    }

    /***
     * 删除产品类型蒙板
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteCreationMask(Request $request)
    {
        $v=array(
            'id'=>'required|integer|min:1',
        );
        $validator=$this->creationMasksSer->store($request,$v);
        $id = intval(trim($request->input("id")));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                $result["state"] = -2;
                $result["message"] = "编号不正确";
                return response()->json($result);
            }
        }
        $creation_mask =  $this->creationMasksSer->getCreationMaskById($id);
        if(empty($creation_mask)){
            $result["state"] = -3;
            $result["message"] = "找不到数据";
            return response()->json($result);
        }
        $res =  $this->creationMasksSer->deleteCreationMask($id);
        if($res){
            $result["state"] = 1;
            $result["message"] = "删除成功";
            return response()->json($result);
        }else{
            $result["state"] = -1;
            $result["message"] = "删除失败";
            return response()->json($result);
        }
    }
    /**
     * 更新创作类型蒙板
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateCreationMaskPage(Request $request)
    {
        $v=array(
            'id'=>'required|integer|min:1',
        );
        $validator=$this->creationMasksSer->store($request,$v);
        $id = intval(trim($request->input("id")));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                return redirect()->back()->with('error_message', '找不到对应数据');
            }
        }
        $creation_mask =  $this->creationMasksSer->getCreationMaskById($id);
        if(empty($creation_mask)){
            return redirect()->back()->with('error_message', '找不到对应数据');
        }
        $product_temp=$this->productTempSer->getProdTempByProdTempId($creation_mask->product_temp_id);
        $creation_mask->product_temp_name='';
        if(!empty($product_temp)){
            $creation_mask->product_temp_name=$product_temp->name;
        }
        $creation_mask->original_image=ImgTool::getImgUrlByImgPath($creation_mask->original_image);
        $creation_mask->category_name=ProdTempCategoryTool::productTempCategoryNameByProdTempCategoryId($creation_mask->product_temp_category_id);
        $categorys=$this->categoryBackSer->getAllProdTempCategoryList();
        $categorys=empty($categorys)?array():$categorys;
        return view('backstage.prodTempCreationMasks.updateCreationMask')
            ->with('creation_mask',$creation_mask)
            ->with('product_temp',$product_temp)
            ->with('categorys',$categorys);
    }
}